(function ( window, undefined ) {
  
  "use strict";

  var GH = function () {};

  GH.prototype.load = function ( type, id, url, callback ) {
    var obj = document.getElementById( id );
    try {
      obj.parentNode.removeChild( obj )
    } catch(e) {
    }

    var insertEle = null;
    if( !type || type == "js" ) {
      insertEle = document.createElement("script");
      insertEle.setAttribute("type", "text/javascript");
      insertEle.setAttribute("src", url);
    } else if ( type == "css" ) {
      insertEle = document.createElement("link");
      insertEle.setAttribute("rel", "stylesheet");
      insertEle.setAttribute("href", url);
    }
    
    insertEle.setAttribute("id", id);
    insertEle.setAttribute("charset", "utf-8");
    
    document.getElementsByTagName("head")[0].appendChild( insertEle );
    insertEle.onload = function() {
      callback && callback();
    }
  };

  GH.prototype.css = function ( obj, attr, value ) {
    switch( arguments.length ) {
    case 2: 
      if ( typeof arguments[1] === "string" ) {
        return getComputedStyle ( obj, false ) [ attr ]; //1
      } else {
        for ( var i in arguments[1] ) { obj.style[i] = attr[i]; } //3
      }
      break;
    case 3:
      obj.style[attr] = value; //2
      break;
    default:
      console.log( "参数传入错误" );
      break;
  };

  GH.prototype.showMessage = function ( message ) {
    var oMsg = document.createElement( "span" );
    oMsg.innerHTML = message;
    GH.css( oMsg, {
     "position": "fixed", "top": "50%", "left": "50%", "WebkitTransform": "translate(-50%,-50%)", "msTransform": "translate(-50%,-50%)", "zIndex": 9999,
     "backgroundColor": "rgba(51,51,51,.7)", "color": "#fff", "padding": ".8em 1em", 
     "border-radius": ".3em", "boxShadow": "0 0 2px 2px rgba(0,0,0,.3)",
     "transition": "all 0.5s ease-in-out",
     "WebkitTransition": "all 0.5s ease-in-out",
     "MsTransition": "all 0.5s ease-in-out",
    });
    document.body.appendChild( oMsg );
    setTimeout( function(){ 
      GH.css( oMsg, {"opacity": 0} );
      setTimeout( function(){ document.body.removeChild( oMsg ) }, 500);
    }, 2000 )
  };

  GH.prototype.delegate = function ( type, obj, target, callback ) {
    obj.addEventListener( type, fun, false );
    function fun ( e ) {
      var t = e.target;
      while ( t ) {
        if ( t.nodeName.toLowerCase() === target.toLowerCase() ) {
          callback && callback.apply( t );
        }
        t = t.parentNode;
      }
    }
  };

  GH.prototype.ajax = function ( url, json, fnSucc, type ) {
    var oAjax;
    type = type ? type : "post";
    //创建ajax对象
    oAjax = new XMLHttpRequest();
    //发送的数据
    var data = "";
    for(var i in json)
    {
      data += i + "=" + json[i] + "&";
    }
    data = data.replace(/&$/,"");
    var data = GH.toArray( json, 0, "", [] ).join("&");
    //GET或者POST
    if(type == "get") {
      oAjax.open("GET", url+"?"+data+new Date().getTime(), true);
      oAjax.send();
    } else if (type == "post") {
      oAjax.open("POST", url, true);
      oAjax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      oAjax.send(data);
    }
    oAjax.onreadystatechange = function() {
      if( oAjax.readyState == 4 && oAjax.status == 200 ) {
        try {
          //返回的json字符串转换为对象
          fnSucc( JSON.parse(oAjax.responseText) );
        } catch(e) {
          fnSucc( oAjax.responseText );
        };
      }
    }
  };

  GH.prototype.serialize = function( form ) {
    form = (typeof form === "string") ? document.getElementById( form ) : form;

    var input = form.getElementsByTagName( "input" ),
        select = form.getElementsByTagName( "select" ),
        textarea = form.getElementsByTagName( "textarea" );
        checkedBox = "",
        res = {}; //存放结果数据

    //遍历input类型  
    for(var i=0, inputSelf; inputSelf=input[i]; i++) {
    
      var inputType = inputSelf.getAttribute("type");
      switch ( inputType ) 
      {
        case "checkbox":
        {
          if ( inputSelf.checked ) { checkedBox += (inputSelf.value+",");}
          res[ inputSelf.getAttribute("name") ] = checkedBox;
        }
        case "radio":
        {
          if( inputSelf.checked ) { res[ inputSelf.getAttribute("name") ] = inputSelf.value; }
          break;
        }
        case "submit":
          break;
        default:
        {
          res[ inputSelf.getAttribute("name") ] = inputSelf.value;
          break;
        }
      }
    };
    //遍历下拉列表
    for ( var i=0, selectSelf; selectSelf=select[i]; i++ ) {
      res[ selectSelf.getAttribute("name") ] = selectSelf.value;
    };
    //遍历多行文本框
    for ( var i=0, textareaSelf; textareaSelf=textarea[i]; i++ ) {
      res[ textareaSelf.getAttribute("name") ] = textareaSelf.value;
    };
    return res;
  };
  
  window.GH = new GH();
})( window ) 
